home *** CD-ROM | disk | FTP | other *** search
/ PC-Blue - MS DOS Public Domain Library / PC-Blue MS-DOS Public Domain Library - NYACC.iso / vol033 / randnorm.bas < prev    next >
Encoding:
BASIC Source File  |  1987-01-11  |  1.4 KB  |  46 lines

  1. 100 CLS:PRINT "RANDNORM.BAS - to calculate 1000 random normal variables"
  2. 110 PRINT "               mean = 0    variance = 1  and check them"
  3. 120 PRINT STRING$(80,"-")
  4. 130 '
  5. 140 OPTION BASE 1
  6. 150 DEFINT I:  DEFSNG R,S,V,X
  7. 160 DIM RN(1000)
  8. 170 RANDOMIZE: PRINT
  9. 175 COLOR 27,0: LOCATE 20,25:PRINT "Please wait...  calculating"
  10. 180 FOR I=1 TO 999 STEP 2
  11. 190 V1 = (2!*RND(1))-1!
  12. 200 V2 = (2!*RND(1))-1!
  13. 210 S = (V1*V1) + (V2*V2)
  14. 220 IF S > 1! THEN 190
  15. 230 X1 = V1 * SQR(-2!*LOG(S)/S)
  16. 240 X2 = V2 * SQR(-2!*LOG(S)/S)
  17. 250 '  x1 and x2 are random normals
  18. 260 RN(I) = X1:  RN(I+1) = X2
  19. 270 '   PRINT USING "####";I;
  20. 280 NEXT I
  21. 290 '
  22. 295    LOCATE 20,25: COLOR 7,0: PRINT SPC(50)
  23. 300    PRINT:PRINT "              1000 variables calculated !!!"
  24. 310 '
  25. 320 REM  check mean and variance
  26. 325    LOCATE 20,25: COLOR 27,0: PRINT "Please be patient... thinking...."
  27. 330 '
  28. 340 SUM = 0!: SUMSQ = 0!: RMAX = 0!: RMIN = 1E+10
  29. 350 FOR I=1 TO 1000
  30. 360 SUM = SUM + RN(I)
  31. 370 IF RN(I) > RMAX THEN RMAX = RN(I)
  32. 380 IF RN(I) < RMIN THEN RMIN = RN(I)
  33. 390 SQ = RN(I)*RN(I)
  34. 400 SUMSQ = SUMSQ + SQ
  35. 410 '   PRINT USING "####";I;
  36. 420 NEXT I
  37. 425 LOCATE 20,25: COLOR 7,0: PRINT SPC(50)
  38. 430 '
  39. 440 REM  output results
  40. 450 '
  41. 460 RMEAN = SUM/1000
  42. 470 PRINT:PRINT:PRINT "Mean = ";: PRINT USING "#.####";RMEAN
  43. 480 VAR = (SUMSQ - (SUM*SUM/1000))/999
  44. 490 PRINT "Variance = ";VAR
  45. 500 END
  46. NT:PRINT "Mean = "